<?php

namespace App\Http\Requests\Customers;

use App\Http\Requests\ApiRequest;
use App\Models\Customers\Customer;
use App\Services\EasySms\EasySmsService;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;

/**
 * @property string $account
 * @property string $type
 * @property string $password
 * @property string dial_code
 */
class CustomerLoginRequest extends ApiRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        $rules = [
            'type'          => ['required', 'string', Rule::in(Customer::LOGIN_TYPES)],
            'password'      => 'required|min:6',
            'account'       => 'required',
            'dial_code'     => 'nullable|int',
            'oauth_type'    => 'nullable|string',
            'oauth_user_id' => 'nullable|int|min:1',
        ];

        return $rules;
    }

    public function afterValidator(Validator $validator)
    {
        if (! $this->dial_code) {
            $this->dial_code = EasySmsService::COUNTRY_CODE_CN;
        }
    }
}
